package com.pzhu.record.controller;

import com.pzhu.record.constants.ExcelReader;
import com.pzhu.record.constants.ExcelWriter;
import com.pzhu.record.entity.Basicinformation;
import com.pzhu.record.entity.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * 学生信息控制类
 *
 * @author Zhou YuSen
 * @date 2020/4/12 17:25
 */
@Controller
@RequestMapping("/basicinformation")
public class BasicinformationController extends BaseAction {
    /**
     * 添加学生信息
     *
     * @param basicinformation
     * @param model
     * @return
     */
    @RequestMapping("/insert")
    public String insert(Basicinformation basicinformation, Model model) {
        int insert = basicinformationService.insertSelective(basicinformation);
        if (insert != 1) {
            model.addAttribute("message", "添加失败");
        } else {
            model.addAttribute("message", "添加成功");
        }
        return "system";
    }

    /**
     * 补充学生信息
     * 更新
     * @param basicinformation
     * @param model
     * @return
     */
    @RequestMapping("/updateStu")
    public String updateStu(Basicinformation basicinformation, Model model) {
        int update = basicinformationService.update(basicinformation);
        if (update != 1) {
            model.addAttribute("message", "补充失败");
        } else {
            model.addAttribute("message", "补充成功");
        }
        return "system";
    }




    /**
     * 批量添加学生信息数据
     *
     * @param student
     * @param model
     * @return
     */
    @RequestMapping("/insertExcel")
    public String insertExcel(MultipartFile student, Model model) {
        ExcelReader<Basicinformation> basicinformationExcelReader = new ExcelReader<>();
        List<Basicinformation> basicinformationList = basicinformationExcelReader.readExcel(student, new Basicinformation());
        int insertBatch = basicinformationService.insertBatch(basicinformationList);

        if (insertBatch == 0) {
            model.addAttribute("messageBasicinformation", "添加失败");
            return "system";
        }
        model.addAttribute("messageBasicinformation", "添加成功");
        return "system";
    }

    /**
     * 分页条件查询
     *
     * @param request
     * @param model
     * @return
     */
    @RequestMapping(value = "/selectLimit")
    public String selectPageUseDyc(HttpServletRequest request, Model model) {
        int pageIndex = 0;
        String keywords = request.getParameter("keywords");
        if (request.getParameter("pageIndex") != null) {
            pageIndex = Integer.parseInt(request.getParameter("pageIndex"));
        }

        Basicinformation basicinformation = new Basicinformation();

        if (keywords != null) {
            keywords = "%" + keywords + "%";
            basicinformation.setBasicinformationName(keywords);
        }
        Page<Basicinformation> basicinformationPage = basicinformationService.selectPage(pageIndex, basicinformation);

        model.addAttribute("basicinformationPage", basicinformationPage);

        return "system";
    }

    /**
     * 导出信息
     *
     * @return
     */
    @RequestMapping(value = "/selectExport")
    public String selectPageUseDyc(@RequestParam Map<String, String> keywords, HttpServletResponse response) {

        if (keywords.size() != 0) {
            for (String value : keywords.keySet()) {
                System.out.println(value);
                if ("".equals(keywords.get(value))) {
                    keywords.put(value, "%" + keywords.get(value) + "%");
                }
            }
        }
        List<Basicinformation> basicinformations = basicinformationService.selectExport(keywords);
        String excelName = "导出学生信息表";
        ExcelWriter<Basicinformation> excelDataVoExcelWriter = new ExcelWriter<Basicinformation>();
        excelDataVoExcelWriter.export(basicinformations, excelName, response, new Basicinformation());

        return "system";
    }


    @PostMapping("/selectById")
    public
    @ResponseBody Basicinformation selectById(@RequestParam(value = "searchId",required = true) String searchId) {
        Basicinformation basicinformation = basicinformationService.selectById(searchId);
        System.out.println(basicinformation);
        return basicinformation;
    }

}
